<template>
	
	<view class="login">
		<form @submit="formSubmit">
		<view class="content-wrapper">
			<view class="title">
				<!-- <h1>欢迎登录</h1> -->
			</view>
		<!-- 	<view style="text-align: center;" >
				<image src="../../static/panpan.jpg"  style="width: 160rpx;height: 160rpx;"></image>
			</view> -->
			
		
				<view class="login-form">
					<view class="login-form-items">
						<view class="login-form-items-title">  <image src="../../static/login.png" style="width: 50rpx;height: 50rpx;"></image></view>
						<input type="text" class="login-input" name="username" placeholder="请输入登录账号" />
					</view>
				
					<view class="login-form-items">
						<view class="login-form-items-title"><image src="../../static/pwd.png" style="width: 50rpx;height: 50rpx;"></image></view>
						<input type="password" name="password" class="login-input"    placeholder="请输入登录密码" />
					</view>
				           
				</view>
			
		</view>
		<div class="submit-wrapper">
			<button type="primary" form-type="submit" class="login-btn">登录</button>
			
		    <!-- <button class="sys_btn"  lang="zh_CN" @click="appLoginWx">微信一键登录</button> -->
			
			<button type="primary" @click="appLoginWx"  style="margin-top: 35rpx;background-color: #4CD964;">微信一键登录</button>
		</div>
		
		</form>
		<!-- <button type="warn" @click="register" style="margin: 20rpx 20rpx 20rpx 20rpx;" class="login-btn">注册</button> -->
		
	</view>
</template>

<script>
	export default {
		props:{
			backpage:'',
			backtype:''
		},
		name:"login",
		data() {
			return {
				
			};
		},
		methods:{
			
			formSubmit: function(e) {
				
				              if(e.detail.value.username==''||e.detail.value.username==undefined){
								  uni.showToast({
								  	title:'用户名不能为空！'
								  })
								  return false
							  }
							  if(e.detail.value.password==''||e.detail.value.password==undefined){
							  									  uni.showToast({
							  									  	title:'密码不能为空！'
							  									  })
							  									  return false
							  }
							let formdata = e.detail.value
							this.login(formdata)
							
							
						},
			async login(formdata) {
							const {data:res} =await uni.$http.post('/sys_user/login_mobile',formdata);
						    if(res.code=='0'){
								let user = res.data;
							
								uni.setStorageSync('userKey',user);
								uni.setStorageSync('authorization',user.authorization);
								this.$emit('loginBack')
							}else if(res.code =='-1'){
			
								uni.showToast({
									  	title:res.message
								})
							}
						},
			
			
			appLoginWx(){
			      uni.getUserProfile({
			      					desc: '获取您的昵称、头像、地区及性別',
			      					success: (res) => {
			      						if (res.errMsg === 'getUserProfile:ok') {
			                                  // 获取到的当前数据存入缓存
			      						      console.log('res.userInfo',res.userInfo)
											  uni.showLoading({
											  	title:'正在登录...'
											  })
											  uni.login({
											    provider: 'weixin',
											    success:(loginInfo)=> {
											      console.log(' uni.login',loginInfo);
												  //this.loginIn(res.userInfo,loginInfo);
												 
												  uni.request({
												  	method:'POST',
													url:uni.$http.baseUrl+'/sys_user/login_wx',
													data:{
														code:loginInfo.code,
														nickName:res.userInfo.nickName,
														gender:res.userInfo.gender,
														city:res.userInfo.city,
														country:res.userInfo.country,
														province:res.userInfo.province,
														language:res.userInfo.language,
														avatarUrl:res.userInfo.avatarUrl
													},
													success: (resSuccess) => {
														console.log('resSuccess',resSuccess);
														
														let user = resSuccess.data.data;
														
														if(undefined==user.username){
															console.log('1111')
															//未绑定用户名
															uni.navigateTo({
																url:'/subpkg/my-bind/my-bind?user='+JSON.stringify(user)+'&backpage='+this.backpage+'&backtype='+this.backtype
															})
															uni.hideLoading();
														}else{
															uni.setStorageSync('userKey',user);
															uni.setStorageSync('authorization',user.authorization);
															this.$emit('loginBack')
															uni.hideLoading();
														}
														
														
														
													},
													fail: (resFail) => {
														console.log(resFail)
													}
												  })
											    }
											  })
											  
			                               
			      						} else {
			      							uni.showToast({
			      								title: '授权失败',
			      								icon: 'error'
			      							});
			      						}
			      					},
			      					fail: err => {
			      						console.log('userInfo-err', JSON.stringify(err));
			      					}
			      				});
			             
			}
		},
		async loginIn(userinfo,loginInfo){
			const {data:res} =await uni.$http.post('/sys_user/login_mobile',{
				code:loginInfo.code,
				nickName:userinfo.nickName,
				gender:userinfo.gender,
				city:userinfo.city,
				country:userinfo.country,
				province:userinfo.province,
				language:userinfo.language,
				avatarUrl:userinfo.avatarUrl
			});
			console.log(res)
		},
		
	}
</script>

<style lang="scss">
page {
		background: #F4F5F6;
	}

	.login {
		.content-wrapper {
			height: 99%;
			width: 100%;

			.title {
				margin-top: 10rpx;
				width: 100%;
				margin-bottom: 10px;

				h1 {
					border: 0px;
					width: 50%;
					margin: 0 auto;
					text-align: center;
					border-bottom: 1px solid #E3E3E3;
					line-height: 50px;
					font-size: 17px;
					overflow: hidden;
				}
			}

			.login-form {
				margin: 20px 10px 20px 15px;
				background: #FFFFFF;

				.login-form-items {
					padding: 15px 10px;
					border-bottom: 1px solid #F3F4F5;
					position: relative;
					display: -webkit-flex;
					display: flex;

					.login-form-items-title {
						width: 10%;
						line-height: 22px;
						flex-shrink: 0;
					}

					.login-input {
						width: 100%
					}
				}
			}
		}

		.submit-wrapper {
			padding: 10px;
			padding-top: 10px;
		}

	}
</style>
